import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/networkmanager.dart';
import 'package:ichinesereader/onboard.dart';
import 'package:ichinesereader/pages/common/Error.dart';
import 'package:ichinesereader/pages/common/NoInternet.dart';

class SplashView extends StatefulWidget {
  static const String routeName = '/splash';

  @override
  State<SplashView> createState() => _SplashViewState();
}

class _SplashViewState extends State<SplashView> {
  final GetXNetworkManager _networkManager = Get.put(GetXNetworkManager());

  @override
  void dispose() {
    Get.delete<GetXNetworkManager>();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<GetXNetworkManager>(builder: (builder) {
      if (_networkManager.connectionType.value == 0) {
        return ShowNoInternetErrorPage();
      } else if (_networkManager.connectionType.value == 1 ||
          _networkManager.connectionType.value == 2) {
        return OnBoard();
      } else {
        return ShowErrorPage();
      }
    });
  }

  Scaffold errorView(AsyncSnapshot<Object?> snapshot) {
    return Scaffold(body: Center(child: Text('Error: ${snapshot.error}')));
  }

  Scaffold waitingView() {
    return Scaffold(
        body: Center(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: Center(
              child: CircularProgressIndicator(
                backgroundColor: Get.theme.primaryColorDark,
                strokeWidth: 9.0,
              ),
            ),
          ),
          Text(
            'Loading...',
            style: TextStyle(
              fontFamily: 'Montserrat',
              color: Get.isDarkMode
                  ? Get.theme.primaryColorDark.withOpacity(0.8)
                  : Get.theme.primaryColorDark,
            ),
          ),
        ],
      ),
    ));
  }
}
